"""
頁面的字段的組成是由：
1. 報表的配置
2. 額外添加的字段合成 （ex：日期、時段等等）
"""
import copy
from event_rule.dwc.integrated_report_rule.rule_config import CONFIG
from common.es_core import report_creator
from common.dwc.maestro.const import GAME_DCT


def _get_integrated_product_profit_analysis_key_mapper(index):
    config = CONFIG.get('profit', {})
    key_mapper = report_creator.get_key_mapper_list(config.get('values'), index)
    additional_key_mapper = [{'key': 'day', 'name': "日期"}]
    key_mapper = additional_key_mapper + key_mapper
    return key_mapper


def _get_integrated_user_analysis_key_mapper(index, report_type=None):
    key_mapper = []
    if report_type:
        assert report_type in ('base', 'new', 'stay')
        report_type_list = [report_type]
    else:
        report_type_list = ['base', 'new', 'stay']
    for report_type in report_type_list:
        config_key = '_'.join(['user', report_type, 'analysis'])
        config = CONFIG.get(config_key, {})
        key_mapper += report_creator.get_key_mapper_list(config.get('values'), index)
    additional_key_mapper = [{'key': 'day', 'name': "日期"}]
    key_mapper = additional_key_mapper + key_mapper
    return key_mapper


def _get_integrated_bet_analysis_key_mapper(index, report_type='bet_amount'):
    config = CONFIG.get('bet_analysis', {})
    key_mapper = report_creator.get_key_mapper_list(config.get('values'), index)
    key_mapper_template = dict()
    for mapper in key_mapper:
        if report_type == mapper['key']:
            key_mapper_template = mapper
    key_mapper = [{'key': 'day', 'name': "日期"}]
    for game in GAME_DCT.get(index, {}):
        key_mapper_item = copy.deepcopy(key_mapper_template)
        key_mapper_item.update({'key': game['key'], 'name': game['name']})
        key_mapper.append(key_mapper_item)
    return key_mapper


def _get_integrated_channel_analysis_key_mapper(index, specific_chn=None):
    config = CONFIG.get('channel', {})
    key_mapper = report_creator.get_key_mapper_list(config.get('values'), index)
    if not specific_chn:
        additional_key_mapper = [{'key': 'rank', 'name': "排名"},
                                 {'key': '_chn', 'name': '渠道', 'is_link': True}]
    else:
        additional_key_mapper = [{'key': 'day', 'name': "日期"}]
    key_mapper = additional_key_mapper + key_mapper
    return key_mapper


def _get_integrated_recharge_analysis_key_mapper(index, report_type=None):
    key_mapper = []
    if report_type:
        assert report_type in ('recharge', 'recharge_average')
        report_type_list = [report_type]
    else:
        report_type_list = ('recharge', 'recharge_average')
    for report_type in report_type_list:
        config = CONFIG.get(report_type, {})
        key_mapper += report_creator.get_key_mapper_list(config.get('values'), index)
    additional_key_mapper = [{'key': 'day', 'name': '日期'}]
    key_mapper = additional_key_mapper + key_mapper
    return key_mapper


def _get_integrated_withdraw_analysis_key_mapper(index):
    config = CONFIG.get('withdraw', {})
    key_mapper = report_creator.get_key_mapper_list(config.get('values'), index)
    additional_key_mapper = [{'key': 'day', 'name': '日期'}]
    key_mapper = additional_key_mapper + key_mapper
    return key_mapper


KEY_MAPPER_FUNC = {
    '5001': _get_integrated_product_profit_analysis_key_mapper,  # 產品營收
    '5002': _get_integrated_user_analysis_key_mapper,  # 用戶整體分析
    '5003': _get_integrated_bet_analysis_key_mapper,  # 玩法分析
    '5004': _get_integrated_channel_analysis_key_mapper,  # 渠道分析
    '5005': _get_integrated_recharge_analysis_key_mapper,  # 充值分析
    '5006': _get_integrated_withdraw_analysis_key_mapper  # 提現分析
}
